package com.wxzl.community.common.widget

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.wxzl.community.common.R
import com.wxzl.community.common.databinding.DialogCommonBinding

/**
 * @Author: LiaoZhongKai
 * @Date: 2021/4/30 9:59
 * @Description: 通用弹框
 */
class CommonDialog(): DialogFragment() {

    private lateinit var mBinding: DialogCommonBinding

    var mContent: String? = null
    @ColorRes var mContentColor: Int? = null
    var mContentSize: Float? = null
    var mNegativeBtnText: String? = null
    var mPositiveBtnText: String? = null
    @ColorRes var mNegativeBtnColor: Int? = null
    @ColorRes var mPositiveBtnColor: Int? = null
    var mNegativeClickListener: OnClickListener? = null
    var mPositiveClickListener: OnClickListener? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.TransBgDialog)
    }

    override fun onStart() {
        super.onStart()
        val dialog = dialog
        if (dialog != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
                val windowManager = requireActivity().getSystemService(Context.WINDOW_SERVICE) as WindowManager
                val metrics = windowManager.currentWindowMetrics
                //宽度设为屏幕宽度的80%
                dialog.window?.setLayout( (metrics.bounds.width() * 0.8).toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
            }else{
                val metrics = DisplayMetrics()
                requireActivity().display?.getRealMetrics(metrics)
                //宽度设为屏幕宽度的80%
                dialog.window?.setLayout( (metrics.widthPixels * 0.8).toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
            }
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mBinding = DataBindingUtil.inflate(inflater,R.layout.dialog_common,container,false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initEvent()
    }

    private fun initView(){

        mBinding.commonContentTv.apply {
            if (mContent.isNullOrEmpty()){
                this.visibility = View.GONE
            }else{
                this.text = mContent
                mContentSize?.let {
                    this.setTextSize(TypedValue.COMPLEX_UNIT_DIP,it)
                }
                mContentColor?.let {
                    this.setTextColor(it)
                }
            }
        }

        mBinding.commonNegativeBtn.apply {
            if (mNegativeBtnText.isNullOrEmpty()){
                mBinding.commonBtnDivider.visibility = View.GONE
            }else{
                this.text = mNegativeBtnText
                mNegativeBtnColor?.let {
                    this.setTextColor(it)
                }
            }
        }

        mBinding.commonPositiveBtn.apply {
            if (mPositiveBtnText.isNullOrEmpty()){
                mBinding.commonBtnDivider.visibility = View.GONE
            }else{
                this.text = mPositiveBtnText
                mPositiveBtnColor?.let {
                    this.setTextColor(it)
                }
            }
        }
    }

    private fun initEvent(){
        mNegativeClickListener?.let { listener->
            mBinding.commonNegativeBtn.setOnClickListener {
                listener.onClick(this)
            }
        }

        mPositiveClickListener?.let { listener->
            mBinding.commonPositiveBtn.setOnClickListener {
                listener.onClick(this)
            }
        }
    }

    fun setContent(content: String): CommonDialog{
        mContent = content
        return this
    }

    fun setContentSize(size: Float): CommonDialog{
        mContentSize = size
        return this
    }

    fun setContentColor(@ColorRes color: Int): CommonDialog{
        mContentColor = color
        return this
    }

    fun setNegativeBtn(text: String,@ColorRes color: Int?,listener: OnClickListener?): CommonDialog{
        mNegativeBtnText = text
        mNegativeBtnColor = color
        mNegativeClickListener = listener
        return this
    }

    fun setPositiveBtn(text: String,@ColorRes color: Int?,listener: OnClickListener?): CommonDialog{
        mPositiveBtnText = text
        mPositiveBtnColor = color
        mPositiveClickListener = listener
        return this
    }

    interface OnClickListener{
        fun onClick(dialogFragment: DialogFragment)
    }
}